//
//  YTInOrderViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/17.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTInOrderViewController.h"
#import <WebKit/WKWebView.h>
#import <WebKit/WebKit.h>
#import "YTLocationManager.h"

@interface YTInOrderViewController ()<WKUIDelegate, WKNavigationDelegate>
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) UIProgressView *progressView;
@property (copy, nonatomic) NSString *urlString;
@end

@implementation YTInOrderViewController

- (void)dealloc {
    
}

- (void)initialize {
    self.naviType = YTNavigationBarDefaultStyle;
    self.navigationItem.title = @"代客下单";
    
    
    WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init];
    webViewConfig.allowsInlineMediaPlayback = true;
    
    // 初始化
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49)
                                  configuration:webViewConfig];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.allowsBackForwardNavigationGestures = true;
    _webView.scrollView.scrollEnabled = true;
    _webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_webView];
    
    // 进度条
    _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 2)];
    _progressView.progressViewStyle = UIProgressViewStyleBar;
    _progressView.progressTintColor = kMainBlueColor;
    _progressView.hidden = true;
    [self.view addSubview:_progressView];
}

- (void)bindViewModel {
    [SVProgressHUD show];
    [[YTLocationManager manager] singleUpdateLocation:^(CLLocation *location, AMapLocationReGeocode *regeocode) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"longitude"] = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
        dict[@"latitude"] = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
        [[YTNetManager manager] postWithURL:@"order/getDriverTakeUrl" parameters:dict success:^(id response) {
            if ([response[@"resultCode"] integerValue] == 1000) {
                NSDictionary *dict = response[@"responseData"];
                NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dict[@"url"]]];
                 [_webView loadRequest:request];
                [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
            } else {
                [SVProgressHUD dismiss];
            }
        } failure:^(NSError *error) {
            [SVProgressHUD dismiss];
        }];
    }];
}

#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [SVProgressHUD dismiss];
    [YTNetManager networkActivityIndicatorDissmis];
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    _progressView.hidden = NO;
    if (![YTNetManager isReachableNetwork]) { //没网
        @weakify(self);
        [self loseConnectionRefreshAction:^{
            @strongify(self);
            [self bindViewModel];
        }];
    } else {
        [YTNetManager networkActivityIndicatorShow];
    }
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [YTNetManager networkActivityIndicatorDissmis];
}

@end
